
//
//  WCNSearchResultsVC.m
//  WCN
//
//  Created by wsj2015 on 16/3/23.
//  Copyright © 2016年 ShanghaiCize Trade And Business Co., Ltd. All rights reserved.
//

#import "WCNSearchResultsVC.h"
#import "WCButton.h"
#import "UINavigationItem+Offset.h"
#import "JCAlertView.h"
#import "WCHospitalCell.h"
#import "WCNNurseModel.h"
#import "WCCareNurseCell.h"
#import "WCNPatientAddVC.h"
#import "WCNNewPICCPreserveVC.h"
#import "WCNCreateMeetingVC.h"
@interface WCNSearchResultsVC ()<UISearchBarDelegate>
@property (nonatomic,  strong) UISearchBar *searchBar;
@property (nonatomic,  strong) NSMutableArray *dataArray;
@property (nonatomic,  strong) NSMutableArray *selectItems;
@property (nonatomic,  strong) NSString *searchContent;



@end

@implementation WCNSearchResultsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self configUI];
    [self postNurseList];
}
- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray =[NSMutableArray array];
    }
    return _dataArray;
}
- (NSMutableArray *)selectItems{
    if (!_selectItems) {
        _selectItems =[NSMutableArray array];
    }
    return _selectItems;
}
- (UISearchBar *)searchBar{
    if (!_searchBar) {
        _searchBar =[[UISearchBar alloc] init];
        _searchBar.frame = CGRectMake(0, 0, kScreenWidth, 44);
        _searchBar.delegate = self;
        //        _searchBar.showsCancelButton = YES;
        _searchBar.barTintColor = [UIColor whiteColor];
        _searchBar.placeholder = @"请输入护理师姓名或者ID";
        _searchBar.searchBarStyle = UISearchBarIconSearch;
        _searchBar.showsScopeBar = YES;
      
    }
    return _searchBar;
}
- (void)setKeyWord:(NSString *)keyWord{
    _keyWord = keyWord;
    self.searchBar.text = keyWord;
    self.searchContent = keyWord;
}
- (void)configUI{
    self.tableView.frame = CGRectMake(0, 44, kScreenWidth, kScreenHeight-64 -44);
    self.title = @"选择护理人";
    if ([self.chooseType isEqualToString:@"inviteMeeting"]) {
        self.title = @"选择邀请人";
    }
    if (!self.onlySelectOne) {
        self.tableView.allowsMultipleSelection = YES;
    }
     [self.view addSubview:self.searchBar];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"WCCareNurseCell" bundle:nil] forCellReuseIdentifier:@"nuresCellId"];
    
    self.searchBar.text = self.keyWord;
    WCButton *rightBtn = [WCButton wcButtonWithType:WCButtonTypeSave];
    [rightBtn addTarget:self action:@selector(save) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationItem addRightBarButtonItem:rightBtn];

    
    
}

#pragma mark -确认选择护理师
- (void)save{
    [self.searchBar resignFirstResponder];
    
    if (self.type ==WCNSearchResultsVCSelectTypeMuti) {
        if (self.selectItems.count==0) {
            [CoreSVP showSVPWithType:CoreSVPTypeCenterMsg Msg:@"至少选一个护理师哦~" duration:1.0f allowEdit:NO beginBlock:nil completeBlock:nil];
            return;
        }
        
    }else{
       
    
    }
    
    
     defineWeakSelf;
     [JCAlertView showTwoButtonsWithTitle:@"提示" Message:@"是否确定选择" ButtonType:1 ButtonTitle:@"继续选择" Click:^{
         
     } ButtonType:0 ButtonTitle:@"是" Click:^{
         if (weakSelf.popCallBack) {
             weakSelf.popCallBack(weakSelf.selectItems);
         }
         for (UIViewController *vc in weakSelf.navigationController.viewControllers)
         {
             
             if ([vc isKindOfClass:[WCNNewPICCPreserveVC class]]) {
                 [weakSelf.navigationController popToViewController:vc animated:YES];
             }
             if ([vc isKindOfClass:[WCNPatientAddVC class]]) {
                 [weakSelf.navigationController popToViewController:vc animated:YES];
             }
             if ([vc isKindOfClass:[WCNCreateMeetingVC class]]) {
                 [weakSelf.navigationController popToViewController:vc animated:YES];
             }
         }
//         [weakSelf.navigationController popViewControllerAnimated:YES];
     }];

}

- (void)postNurseList{
    
    NSMutableDictionary *params =[NSMutableDictionary dictionary];
    self.tableView.loading = YES;
   
    if (self.searchBar.text &&![self.searchBar.text isEqualToString:@""]) {
        params[@"content"] = self.searchBar.text;
    }
    
    if (self.hospitalId) {
        params[@"hospitalId"] = self.hospitalId;
    }
    defineWeakSelf;
    self.tableView.loading = YES;
    CoreSVPLoading(@"加载中...", NO);
   [[WCNetworkAccessManager sharedMg] POST:kSearchNurseList parameters:params success:^(NSDictionary *responseObject) {
       [weakSelf.dataArray removeAllObjects];
       if ([responseObject[@"data"] isKindOfClass:[NSArray class]]) {
          [weakSelf.dataArray addObjectsFromArray:[WCNNurseModel objectArrayWithKeyValuesArray:responseObject[@"data"]]];
           [weakSelf.selectItems removeAllObjects];
           [weakSelf.tableView reloadData];
       }
     
       
   } failure:nil netError:nil always:^{
       weakSelf.tableView.loading= NO;
       [CoreSVP dismiss];
   }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 85;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    WCCareNurseCell *cell =[tableView dequeueReusableCellWithIdentifier:@"nuresCellId" forIndexPath:indexPath];
    cell.nurse =self.dataArray[indexPath.row];
    if (self.unableArray) {
        cell.unableArray = self.unableArray;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   
    WCNNurseModel *model =self.dataArray[indexPath.row];
   
    if (self.onlySelectOne) {
        [self.selectItems removeAllObjects];
    }
    [self.selectItems addObject:self.dataArray[indexPath.row]];
    NSLog(@"%i",self.selectItems.count);
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.onlySelectOne==YES) {
        return;
    }
     NSNumber *num = self.dataArray[indexPath.row];
    [self.selectItems removeObject:num];
     NSLog(@"%i",self.selectItems.count);
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    
    [searchBar resignFirstResponder];
    [self postNurseList];
    
}
@end
